/**
* 业务员-广告-设备管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>设备管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-select size="small" v-model="formInline.key" filterable  placeholder="请选择">
          <el-option label="全部" value=""></el-option>
          <el-option
            v-for="item in parking"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="dataForm" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="50">
      </el-table-column>
      <el-table-column align="center" sortable prop="eId" label="ID" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="parkingLot.plName" label="所属停车场" width="120">
      </el-table-column>
      <el-table-column align="center" sortable prop="eMaxCount" label="最大播放广告" width="120">
      </el-table-column>

      <el-table-column align="center" sortable prop="eIshot" label="是否热点地点" min-width="50">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.eIshot=='1'?nshow:fshow" active-color="#13ce66" inactive-color="#ff4949" @change="editType(scope.$index, scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click='closeDialog("edit")'>
      <el-form label-width="80px" ref="editData" :model="editData" :rules="rules">
        <el-form-item label="最大播放广告" prop="eMaxCount">
          <el-input size="small" v-model="editData.eMaxCount" auto-complete="off" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="所属停车场" prop="plId">
          <el-select size="small" v-model="editData.plId" placeholder="请选择" class="userRole">
            <el-option
              v-for="item in parking"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否热点地点" prop="eIshot">
          <el-radio v-model="editData.eIshot" label="0">非热点</el-radio>
          <el-radio v-model="editData.eIshot" label="1">热点</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 导入请求方法
import {
  userList,
  userSave,
  userDelete,
  userPwd,
  userExpireToken,
  userFlashCache,
  userLock,
  UserDeptTree,
  UserDeptSave,
  UserDeptdeptTree,
  UserChangeDept, saveEq
} from '../../api/userMG'
import Pagination from '../../components/Pagination'
import {postJSON} from "../../api/axiosFun";
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      title: '添加用户',
      editFormVisible: false, //控制编辑页面显示与隐藏
      dataAccessshow: false, //控制数据权限显示与隐藏
      unitAccessshow: false, //控制所属单位隐藏与显示
      parking:[],//下拉框数据
      dataForm: [],
      // 编辑与添加
      editData: {
        eId: '',
        eIshot: '',
        eMaxCount: '',
        plId: '',
        token: localStorage.getItem('logintoken')
      },

      // 选择数据
      selectdata: [],
      // rules表单验证
      rules: {

      },

      // 请求数据参数
      formInline: {
        page: 1,
        limit: 10,
        key: ''
      },

      // 选中
      checkmenu: [],

      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },

  /**
   * 数据发生改变
   */
  watch: {},

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取数据方法
    getdata(parameter) {
      this.loading = true

      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */
        postJSON("/api/equipment/page",parameter).then(res => {
        this.loading = false
        console.log(res)
        if (res.status != 200) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          this.dataForm = res.data.data.page.list
          this.parking = res.data.data.parking
          // 分页赋值
          this.pageparm.currentPage = this.formInline.page
          this.pageparm.pageSize = this.formInline.limit
          this.pageparm.total = res.data.data.page.total
        }
      })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    //搜索事件
    search() {
      this.getdata(this.formInline)
    },

    //显示编辑界面
    handleEdit: function(index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改设备'

        this.editData.eId =row.eId
        this.editData.eIshot =row.eIshot
        this.editData.eMaxCount =row.eMaxCount
        this.editData.plId =row.plId
      } else {
        this.title = '添加设备'

      }
    },
    // 编辑、添加提交方法
    submitForm(editData) {

      console.log(this.editData)
      console.log(editData)
        // 请求方法
      postJSON("/api/equipment/save",this.editData).then(res => {
          this.loading = false
          console.log(res)
          if (res.status != 200) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.$message({
              type: 'success',
              message: "成功"
            })
          }
        })
          .catch(err => {
            this.editFormVisible = false
            this.loading = false
            this.$message.error('保存失败，请稍后再试！')
          })
      this.editData.eId =''
      this.editData.eIshot =''
      this.editData.eMaxCount =''
      this.editData.plId =''
        return false

    },

    handleClick(data, checked, node) {
      if (checked) {
        this.$refs.tree.setCheckedNodes([])
        this.$refs.tree.setCheckedNodes([data])
        this.unitparm.deptId = data.id
        this.unitparm.deptName = data.name
        //交叉点击节点
      } else {
      }
    },

    // 选择复选框事件
    selectChange(val) {
      this.selectdata = val
    },
    // 关闭编辑、增加弹出框
    closeDialog(dialog) {
      if (dialog == 'edit') {
        this.editFormVisible = false
      } else if (dialog == 'perm') {
        this.dataAccessshow = false
      } else if (dialog == 'unit') {
        this.unitAccessshow = false
      }
    },
    // 删除
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 删除
          console.log()
          postJSON(`/api/equipment/del/${row.eId}`,null)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '数据已删除!'
                })

              } else {
                this.$message({
                  type: 'success',
                  message: "成功"
                })
                this.getdata(this.formInline)
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('数据删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除！'
          })
        })
    },
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>

